package cn.zjweu.servlet;

import cn.zjweu.domain.*;
import cn.zjweu.service.CartService;
import cn.zjweu.service.OrderService;
import cn.zjweu.service.UserService;
import cn.zjweu.service.impl.CartServiceImpl;
import cn.zjweu.service.impl.OrderServiceImpl;
import cn.zjweu.service.impl.UserServiceImpl;
import cn.zjweu.utils.BaseServlet;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.math.BigDecimal;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.TimeZone;

/*Designed By YeJianFeng-2022b32057*/
@WebServlet("/order/*")
public class OrderServlet extends BaseServlet {

    private OrderService orderService = new OrderServiceImpl();
    private CartService cartService = new CartServiceImpl();
    private UserService userService = new UserServiceImpl();

    public void submitOrder(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        int userId = 0;

        try {
            userId = Integer.parseInt(request.getParameter("userId"));
            String cartList = request.getParameter("cartList");
            System.out.println("cartList: " + cartList);
            BigDecimal totalPrice = new BigDecimal(request.getParameter("totalPrice"));
            User user = userService.findUserById(userId);

            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            format.setTimeZone(TimeZone.getTimeZone("GMT+8"));
            Order order = new Order(null, user, format.format(new Date()), null, 0, new BigDecimal(0), totalPrice, null);
            if (orderService.saveOrder(userId, order, cartList)) {
                System.out.println("订单提交成功");

                info.setFlag(MessageInfo.SUCCESS);
                info.setData(order);
                info.setInfoMsg("订单提交成功！");
            } else {
                System.out.println("订单提交失败");

                info.setFlag(MessageInfo.FAIL);
                info.setData(order);
                info.setInfoMsg("订单提交失败！");
            }
        } catch (Exception e) {
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("数据错误！");
        } finally {
            writeValue(info, response);
        }
    }

    public void changeOrderStatus(HttpServletRequest request, HttpServletResponse response) throws IOException {
        ResultInfo info = new ResultInfo();
        int orderId = 0;
        int status = 0;
        try {
            orderId = Integer.parseInt(request.getParameter("orderId"));
            status = Integer.parseInt(request.getParameter("status"));
            if (orderService.changeOrderStatusByOrderId(orderId, status)){
                info.setFlag(MessageInfo.SUCCESS);
                info.setData(orderId);
                info.setInfoMsg("修改成功");
            }
        }catch (Exception e){
            info.setFlag(MessageInfo.FAIL);
            info.setInfoMsg("修改失败");
        }finally {
            writeValue(info,response);
        }
    }

    public void findOrderListByPage(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String currentPageStr = request.getParameter("currentPage");
        String pageSizeStr = request.getParameter("pageSize");
        String username = request.getParameter("username");
        int currentPage = MessageInfo.DEFAULT_CURRENT_PAGE;
        if (currentPageStr != null && currentPageStr.length() > 0) {
            currentPage = Integer.parseInt(currentPageStr);
        }

        int pageSize = MessageInfo.DEFAULT_PAGE_SIZE;
        if (pageSizeStr != null && pageSizeStr.length() > 0) {
            pageSize = Integer.parseInt(pageSizeStr);
            if (pageSize < 1) {
                pageSize = MessageInfo.DEFAULT_PAGE_SIZE;
            }
        }

        PageBean<Order> pageOfOrder = orderService.findOrderListByPage(currentPage, pageSize, username);
        writeValue(pageOfOrder, response);

    }
}
